package com.school.controller;

import com.alibaba.fastjson.JSONObject;
import com.school.entity.CertificateHonor;
import com.school.service.CertificateHonorService;
import com.school.untils.ResultEntity;
import com.school.untils.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

@Api(tags = "荣誉证书信息管理")
@RestController
@RequestMapping("/certificateHonor")
public class CertificateHonorController {
    @Autowired
    CertificateHonorService certificateHonorService;

    // 增加
    @ApiOperation("荣誉证书信息增加")
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public ResultEntity addAcademicLevel(HttpServletRequest servletRequest) throws IOException {
        List<MultipartFile> fileList = null;
        if (servletRequest instanceof StandardMultipartHttpServletRequest) {
            MultipartHttpServletRequest multipartRequest = WebUtils.getNativeRequest(servletRequest, MultipartHttpServletRequest.class);
            fileList = multipartRequest.getFiles("file");
        }
        JSONObject userJsonObject = (JSONObject) JSONObject.parse(servletRequest.getParameter("certificateHonor"));
        CertificateHonor certificateHonor = JSONObject.toJavaObject(userJsonObject, CertificateHonor.class);
        return  ResultUtils.build(certificateHonorService.addCertificateHonor(fileList, certificateHonor));
    }

    // 更新
    @ApiOperation("荣誉证书信息更新")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public ResultEntity updateAcademicLevel(@RequestBody CertificateHonor certificateHonor){
        return ResultUtils.build(certificateHonorService.updateCertificateHonor(certificateHonor));
    }

    // 根据id查看详情
    @ApiOperation("荣誉证书信息查看详情")
    @GetMapping("/getDetailsById/{id}")
    public ResultEntity getDetailsById(@PathVariable("id") String id){
        return ResultUtils.build(certificateHonorService.getDetailsById(id));
    }

    // 根据id删除
    @ApiOperation("荣誉证书信息删除")
    @DeleteMapping("/delete/{id}")
    public void deleteAcademicLevel(@PathVariable("id") String id){
        certificateHonorService.deleteCertificateHonor(id);
    }

    // 根据id批量删除
    @ApiOperation("荣誉证书信息批量删除")
    @DeleteMapping("/batch")
    public void deleteCertificateHonorList(String idList){
        certificateHonorService.deleteCertificateHonorList(idList);
    }

    // 根据条件分页查询
    @ApiOperation("根据条件分页查询")
    @GetMapping("/specificPage")
    public ResultEntity specificPageQueryCertificateHonorList(CertificateHonor certificateHonor,@RequestParam int page,@RequestParam int size){
        return ResultUtils.build(certificateHonorService.specificPageQueryCertificateHonorList(certificateHonor,page,size));
    }

}
